//
//  ARBuyViewController.m
//  AriseSamePro
//
//  Created by 包日守 on 15/11/3.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARBuyViewController.h"
#import "ARBuyerCell.h"
#import "ARPictureController.h"

@interface ARBuyViewController ()<UIViewControllerPreviewingDelegate>

@end

@implementation ARBuyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
        NSLog(@"3D Touch is available!");
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIViewControllerPreviewingDelegate
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)
{
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
    
    ARBuyerCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    NSLog(@"%ld",indexPath.section);
    
    CGPoint convertedLocation = [self.tableView convertPoint:location toView:cell.scrollView];
    
      if (CGRectContainsPoint(cell.scrollView.bounds, convertedLocation))
      {
          NSMutableArray *marray = [NSMutableArray array];
          for(UIImageView *imageV in cell.marrayOfImages)
          {
          
              if(imageV.image){
                  [marray addObject:imageV.image];
              }
              else
              {
              
                  [marray addObject:[UIImage new]];
              }
              
              
          }
          
          ARPictureController *pVC = [[ARPictureController alloc] init];
          pVC.arrayOfImages = marray;
          pVC.index = cell.pageControl.currentPage;
          
          return pVC;
        
      
      }
    
    return nil;
    

}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)
{
    [self presentViewController:viewControllerToCommit animated:NO completion:nil];
}


#pragma mark - Table view data source


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    float hight = 0;
    
    ARCate5Model *model = self.cateModels[indexPath.section];
    //CGFloat w = [UIScreen mainScreen].bounds.size.width - 25;
    CGRect centextRect = [model.mediaModel.productModel.desc boundingRectWithSize:CGSizeMake(328, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    
    hight = centextRect.size.height;
    
    if ([model.mediaModel.productModel.desc isEqualToString:@""]) {
        hight = 0;
    }

    
    return hight + 507 - 21;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARBuyerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ARBuyerCell"];
    
    if (cell == nil) {
        
        
        [tableView registerNib:[UINib nibWithNibName:@"ARBuyerCell" bundle:nil] forCellReuseIdentifier:@"ARBuyerCell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"ARBuyerCell"];
        
        
    }
    
    cell.cateModel = self.cateModels[indexPath.section];
    
    [cell setShareBtnBlock:^(ARCate5Model *cateModel, UIButton *sender) {
        [self createShare:cateModel WithSender:sender];
        
    }];
    
    __weak typeof(cell) wc = cell;
    
    [cell setTapImageViewsBlock:^(int index) {
        NSMutableArray *marray = [NSMutableArray array];
        for(UIImageView *imageV in wc.marrayOfImages)
        {
            
            if(imageV.image){
                [marray addObject:imageV.image];
            }
            else
            {
                
                [marray addObject:[UIImage new]];
            }
            
            
        }
        
        ARPictureController *pVC = [[ARPictureController alloc] init];
        pVC.arrayOfImages = marray;
        pVC.index = wc.pageControl.currentPage;
        
        
        [self presentViewController:pVC animated:NO completion:nil];
        
    }];
    
    return cell;
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
